<template>
  <div>
    <h1 style="margin:10px auto;text-align:center;">我的分享</h1>  
    <van-form ref="form" :model="form" :rules="rules" @submit="onSubmit">
      <van-field
          v-model="form.content"
          rows="1"
          autosize
          label="发表内容"
          type="textarea"
          placeholder="请输入"
        />   

      <van-field name="fileList" label="照片上传">
        <template #input>
         <van-uploader  v-model="fileList" multiple :max-count="2" :after-read="afterRead" :max-size="2 * 1024 * 1024" @oversize="onOversize"/> 
        </template>
      </van-field>

      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">提交</van-button>
        <van-button round block type="gray" style="margin-top:10px;" @click="$router.push('/share')">返回</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import httpApi from '@/http';


  export default {
    data() {
    return {
      fileList: [],
      form:{
        user_id:1,
        ptime:this.ptime,
        content:'',
        img1:'',
        img2:''
      },
      rules:{
          user_id:[{required:true,message:'必填',trigger:'blur'}],
          ptime:[{required:true,message:'必选',trigger:'blur'}],
          content:[{required:true,message:'必填',trigger:'blur'}],
          img1:[{required:true,message:'必选',trigger:'change'}],
          img2:[{required:true,message:'必选',trigger:'change'}],
        },
    };
  },

  methods: {
    onSubmit(values) {
      var date =new Date()
      let year=date.getFullYear()
      let month=(date.getMonth() +1)<10?'0'+(date.getMonth() +1):(date.getMonth() +1)
      let data =date.getDate()<10?'0'+date.getDate():date.getDate()
      this.ptime=year+'-'+month+'-'+data
      
      httpApi.PostbarApi.add(this.form).then(res=>{
        console.log(res);
      })
      console.log('submit', values);   
      // this.$router.push('/share')
    },
    afterRead(fileList) {

      // 此时可以自行将文件上传至服务器
      console.log(fileList);
    },
    onOversize(fileList) {
      console.log(fileList);
      Toast('文件大小不能超过 2MB');
    },
  },
  }
</script>

<style lang="scss" scoped>

</style>